﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml.Linq;
using HIWSystem.Common;

namespace HIWSystem.CustomControls
{
    /// <summary>
    /// AutoGenerateIMDFrequencyBandButton.xaml 的交互逻辑
    /// </summary>
    public partial class AutoGenerateIMDFrequencyBandButton : UserControl
    {
        public AutoGenerateIMDFrequencyBandButton()
        {
            InitializeComponent();
        }
        //站位名称
        public string StationName
        {
            get { return (string)GetValue(StationNameProperty); }
            set { SetValue(StationNameProperty, value); }
        }

        // Using a DependencyProperty as the backing store for StationName.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty StationNameProperty =
            DependencyProperty.Register("StationName", typeof(string), typeof(AutoGenerateIMDFrequencyBandButton), new PropertyMetadata(string.Empty));


        //选择的机种
        public string SelectedPN
        {
            get { return (string)GetValue(SelectedPNProperty); }
            set { SetValue(SelectedPNProperty, value); }
        }

        // Using a DependencyProperty as the backing store for SelectedPN.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedPNProperty =
            DependencyProperty.Register("SelectedPN", typeof(string), typeof(AutoGenerateIMDFrequencyBandButton), new PropertyMetadata(string.Empty, new PropertyChangedCallback(OnSelectedNewPN)));


        private static void OnSelectedNewPN(System.Windows.DependencyObject d, System.Windows.DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
            {
                string strPN = e.NewValue.ToString();
                if (strPN != "")
                {
                    //清空StackPanel控件中的内容
                    ((AutoGenerateIMDFrequencyBandButton)d)?.WrapPanel?.Children?.Clear();
                    //判断机种是否配置
                    if (System.IO.File.Exists(System.IO.Path.Combine(Global.IMDEntryInfoConfigDirectory,strPN+".xml")))
                    {
                        //配置文件中配置的自动生成按钮的名称
                        string strAutoGenerateButtonNames = string.Empty;
                        XDocument doc = XDocument.Load(System.IO.Path.Combine(Global.IMDEntryInfoConfigDirectory, strPN + ".xml"));
                        strAutoGenerateButtonNames = doc.Element("EntryInfos").Element("EntryInfo").Element("AutoGenerateButtonNames").Value;
                        //获取按钮名称列表
                        List<string> lstConfigGenerateButtonNames = strAutoGenerateButtonNames.Split('|').ToList<string>();
                        for (int i = 0; i < lstConfigGenerateButtonNames.Count; i++)
                        {
                            var button = new Button();
                            button.Content = lstConfigGenerateButtonNames[i];
                            button.Click += (x, y) => {
                                ((AutoGenerateIMDFrequencyBandButton)d).StationName = ((Button)x).Content.ToString();
                            };
                                button.Margin = new Thickness(3, 3, 3, 3);
                            button.Width = 80;
                            ((AutoGenerateIMDFrequencyBandButton)d)?.WrapPanel?.Children?.Add(button);
                        }
                    }
                   
                }
            }
        }


    }
}
